import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

//Should be JPanel later on, JFrame just for prototype.
public class PrototypeFlashReaction extends JFrame implements KeyListener{
	private long endTime;
	private long startTime, startColorTime;
	private static int leftKey;
	private boolean changedColor, success, gameEnd;
	
	private static int reward, punishment;
	
	public PrototypeFlashReaction(int lKey){
		reward = 1;
		punishment = 1;
		success = false;
		changedColor = false;
		gameEnd = false;
		leftKey = lKey;
		
		setSize(400,300);
		getContentPane().setBackground(Color.black);
		addKeyListener(this);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		double waitTime = 2 * Math.random() + 6;
		long startColorTime = System.nanoTime();
		while((System.nanoTime() - startColorTime)/1000000000 < waitTime){
		}
		changeColor();
	}
	
	public void changeColor(){
		getContentPane().setBackground(Color.white);
		startTime = System.nanoTime();
		changedColor = true;
	}

	public static void main(String[] args){
		PrototypeFlashReaction p = new PrototypeFlashReaction(KeyEvent.VK_A);
	}

	public void keyTyped(KeyEvent e) {

	}

	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == leftKey && changedColor && !gameEnd){
			endTime = System.nanoTime();
			if( (endTime-startTime)/1000000000.0 < 0.5){
				success = true;
			}
			else{
				success = false;
			}
			output(success);
		}
		else if(e.getKeyCode() == leftKey && !changedColor && !gameEnd){
			System.out.println("Punishment");
		}
		gameEnd = true;
	}
	
	public void output(boolean s){
		if(s){
			System.out.println("You clicked in " + (endTime-startTime)/1000000000.0 + " seconds. You passed!");
		}
		else{
			System.out.println("You're a moron!  You're slower than an ape.  You have no chance in life. The natives of Ramachallampa have hunted you down. \\n Congratulations, your charred corpse will serve as their dinner.");
		}
	}

	public void keyReleased(KeyEvent e) {
		
	}



}
